/**
 * Created by yx on 2017/12/16.
 */

$.ajax({
    type: 'GET',
    url: '/news/posts/getPostsDtlById?postId=' + sessionStorage.getItem('newsId'),
    success: function(resb) {
        console.log(resb.data);
        var time = resb.data.showDate + '000';
        $('.newsTitle').html(resb.data.postTitle);
        $('.author').html(resb.data.authors.name + '   ' + formatDate(parseInt(time)));
        // $('.content').html(resb.data.postContent);
        if(resb.data.tpl == 'article_news' || resb.data.tpl == 'article_news_text'){
            $('.content').html(resb.data.postContent);
        }else if(resb.data.tpl == 'article_video'){
            var key = JSON.parse(resb.data.postContent),
                template = $('#videoTemplate').clone().html().replace('movie.ogg',key.video_url);
            $('.content').append(template);
        }else if(resb.data.tpl == 'article_pics'){
            var key = JSON.parse(resb.data.postContent),
                news = key.img_news;
            for(var i=0;i<news.length;i++){
                var template = $('#picsTemplate').clone().html().replace('pics',news[i].img).replace('这是一段文本',news[i].content);
                $('.content').append(template)
            }
        }else if(resb.data.tpl == 'article_pics_list'){
            var key = JSON.parse(resb.data.postContent),
                news = key.img_news;
            for(var i=0;i<news.length;i++){
                var template = $('#picsList').clone().html().replace('pics',news[i]);
                $('.content').append(template)
            }
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

$('.back').click(function () {
    loadPage('faceNewsList/faceNewsList.html');
});

/**
 * 时间戳转换成时间
 * @param nows 转换的时间戳
 */
function formatDate(nows) {
    var now=new Date(nows);
    var year=now.getFullYear();
    var month=now.getMonth()+1;
    var date=now.getDate();
    var hour=now.getHours();
    var minute=now.getMinutes();
    var second=now.getSeconds();
    return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}